package services;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;

import com.aton.db.SessionFactory;

import models.DrKeywords;
import models.DrKeywordsExample;
import models.DrVKeywords;
import models.DrVKeywordsExample;
import models.SystemLog;
import models.SystemOperator;
import models.mapper.DrKeywordsMapper;
import models.mapper.DrVKeywordsMapper;
import models.mapper.SystemLogMapper;

/**
 * 关键词服务类
 * @author wangshaoming
 *
 */
public class WordService {
	/**
	 * 获取关键词优化列表
	 * @return
	 */
	public static List<DrVKeywords> listWord() {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSession();
		try{
			DrVKeywordsMapper mapper=ss.getMapper(DrVKeywordsMapper.class);
			DrVKeywordsExample example=new DrVKeywordsExample();
			return mapper.listWord(example);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存优化内容
	 * @param keyword 
	 * @param loginUser
	 * @param ip
	 * @return
	 */
	public static boolean saveKeyWords(DrKeywords keyword,
			SystemOperator loginUser, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			DrKeywordsMapper mapper=ss.getMapper(DrKeywordsMapper.class);
			mapper.insert(keyword);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),ip,"优化关键词","优化关键词",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 清空优化效果
	 * @param loginUser
	 * @param ip
	 * @return
	 */
	public static boolean clearKeyWords(SystemOperator loginUser, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			DrKeywordsMapper mapper=ss.getMapper(DrKeywordsMapper.class);
			//将所有的记录更新为未使用
			DrKeywords staword=new DrKeywords();
			DrKeywordsExample example=new DrKeywordsExample();
			staword.setIsuse(new BigDecimal(0));
			mapper.updateByExampleSelective(staword, example);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),ip,"清空优化","清空优化关键词",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 根据id获取优化记录
	 * @param wordid
	 * @return
	 */
	public static DrKeywords getById(String wordid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSession();
		try{
			DrKeywordsMapper mapper=ss.getMapper(DrKeywordsMapper.class);
			return mapper.selectByPrimaryKey(wordid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 删除优化记录
	 * @param wordid
	 * @param loginUser
	 * @param ip
	 * @return
	 */
	public static boolean deleteKeyWord(String wordid,
			SystemOperator loginUser, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			DrKeywordsMapper mapper=ss.getMapper(DrKeywordsMapper.class);
			mapper.deleteByPrimaryKey(wordid);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 根据id获取文本内容
	 * @param wordid
	 * @return
	 */
	public static String getContentBywordid(String wordid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSession();
		try{
			DrKeywordsMapper mapper=ss.getMapper(DrKeywordsMapper.class);
			return mapper.getContentBywordid(wordid);
		}finally{
			ss.close();
		}
	}
}
